﻿using IOP.SgrA;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VkSample112;

public class FudgeAnimation : Animation
{
    private Uniform Uniform = new Uniform() { YStart = -1.5f, YSpan = 3.0f };
    private float AngleSpan = 0;
    private float AngleStep = 2.0f;
    private ITexture? Texture;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="object"></param>
    /// <exception cref="NotImplementedException"></exception>
    public override void OnAttach(IRenderObject @object)
    {
        RenderObject = @object;
        Texture = RenderObject.GetComponents<ITexture>().Where(x => x.SetIndex == 0 && x.Binding == 0).FirstOrDefault();
    }

    public override void Play(TimeSpan timeSpan)
    {
        AngleSpan = (float)(AngleSpan + ToRadians(AngleStep));
        if (ToDegrees(AngleSpan) > 90) AngleStep = -2.0f * (float)timeSpan.TotalSeconds * 20;
        else if (ToDegrees(AngleSpan) < -90) AngleStep = 2.0f * (float)timeSpan.TotalSeconds * 20;
        Uniform.AngleSpan = AngleSpan;
        Texture?.UpdateTextureMemoryData(Uniform);
    }

    public override void CloneToNewRender(IRenderObject newObj)
    {
        var f = new FudgeAnimation();
        f.Name = Name;
        newObj.AddComponent(f);
        f.OnAttach(newObj);
    }

    /// <summary>
    /// 角度转换成弧度的方法
    /// </summary>
    /// <param name="degree"></param>
    /// <returns></returns>
    private float ToRadians(float degree)
    {
        return degree * MathF.PI / 180;
    }
    /// <summary>
    /// 弧度变换成角度的方法
    /// </summary>
    /// <param name="radian"></param>
    /// <returns></returns>
    private float ToDegrees(float radian)
    {
        return radian * 180 / MathF.PI;
    }
}

public struct Uniform
{
    public float AngleSpan;
    public float YStart;
    public float YSpan;
}
